package cn.liujianbo.example.pattern.strategy.cash;

import java.util.Scanner;

/**
 * @author 刘建波
 * @date 2022-04-02 10:23
 */
public class CashContext {
    private CashInterface instance;

    public CashContext(CashInterface instance) {
        this.instance = instance;
    }

    public double getResult(double money) {
        return instance.acceptCash(money);
    }

    public static void main(String[] args) {
        CashContext cashContext = null;

        Scanner scanner = new Scanner(System.in);

        Integer type = null;
        while (type == null) {
            System.out.println("请输入打折方式（1/2/3）");
            type = scanner.nextInt();
            switch (type) {
                case 1:
                    cashContext = new CashContext(new CashNormal());
                    break;
                case 2:
                    cashContext = new CashContext(new CashRebate(0.8));
                    break;
                case 3:
                    cashContext = new CashContext(new CashReturn(300));
                    break;
                default:
                    System.out.println("请输入1/2/3");
                    break;
            }
        }

        double totalPrices = 0;

        System.out.print("请输入单价：");
        double price = scanner.nextDouble();
        System.out.print("请输入数量：");
        double num = scanner.nextDouble();
        totalPrices = cashContext.getResult(price * num);

        System.out.println("单价：" + price + "，数量：" + num + "，类型：" + type + "，合计：" + totalPrices);

        scanner.close();
    }
}
